package com.yanggu.redisson.config;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yanggu.redisson.domain.Entry;
import com.yanggu.redisson.domain.OrderData;
import org.redisson.api.*;
import org.redisson.api.options.LocalCachedMapOptions;
import org.redisson.codec.TypedJsonJacksonCodec;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

/**
 * Redisson集合型数据结构配置类
 * <p>Redisson所有分布式数据结构都是是无状态对象，线程安全的，数据是存储在Redis中，因此可以直接配置成spring中的bean</p>
 * <p>Redisson实现了Java中常见的集合型数据结构（List、Set、Map、Queue、DelayedQueue、BlockingQueue、BoundedBlockingQueue、PriorityQueue、PriorityBlockingQueue、Deque、BlockingDeque、PriorityDeque、PriorityBlockingDeque）</p>
 */
@Configuration
public class RedissonCollectionConfig {

    //RList
    @Bean("test-list")
    public RList<String> rList(RedissonClient redissonClient) {
        return redissonClient.getList("test-list");
    }

    //RMapCache
    @Bean("test-map-cache")
    public RMapCache<String, String> rMapCache(RedissonClient redissonClient) {
        return redissonClient.getMapCache("test-map-cache");
    }

    //RSetCache
    @Bean("test-set-cache")
    public RSetCache<String> rSetCache(RedissonClient redissonClient) {
        return redissonClient.getSetCache("test-set-cache");
    }

    //RListMultimapCache
    @Bean("test-list-multimap-cache")
    public RListMultimapCache<String, String> rListMultimapCache(RedissonClient redissonClient) {
        return redissonClient.getListMultimapCache("test-list-multimap-cache");
    }

    //RSetMultimapCache
    @Bean("test-set-multimap-cache")
    public RSetMultimapCache<String, String> rSetMultimapCache(RedissonClient redissonClient) {
        return redissonClient.getSetMultimapCache("test-set-multimap-cache");
    }

    //RLocalCachedMap
    @Bean("test-local-cached-map")
    public RLocalCachedMap<String, String> rLocalCachedMap(RedissonClient redissonClient) {
        LocalCachedMapOptions<String, String> options = LocalCachedMapOptions.<String, String>name("test-local-cached-map")
                .cacheSize(1000)
                .timeToLive(Duration.ofMillis(1L))
                .evictionPolicy(LocalCachedMapOptions.EvictionPolicy.LRU);
        return redissonClient.getLocalCachedMap(options);
    }

    //RQueue
    @Bean("test-queue")
    public RQueue<String> rQueue(RedissonClient redissonClient) {
        return redissonClient.getQueue("test-queue");
    }

    //RPriorityBlockingQueue（优先阻塞队列）
    @Bean("test-priority-blocking-queue")
    public RPriorityBlockingQueue<Entry<String>> rPriorityBlockingQueue(RedissonClient redissonClient) {
        //自定义序列化和反序列化策略，使用jackson的方式
        TypedJsonJacksonCodec codec = new TypedJsonJacksonCodec(new TypeReference<Entry<String>>() {});
        return redissonClient.getPriorityBlockingQueue("test-priority-blocking-queue", codec);
    }

    /**
     * 延迟队列。Redisson延迟队列是数据到期后，转移到目标队列中。消费目标队列
     */
    @Bean("test-delayed-queue")
    public RDelayedQueue<OrderData> rDelayedQueue(RedissonClient redissonClient) {
        //自定义序列化和反序列化策略，使用jackson的方式
        TypedJsonJacksonCodec codec = new TypedJsonJacksonCodec(new TypeReference<OrderData>() {});
        RBlockingQueue<OrderData> blockingQueue = redissonClient.getBlockingQueue("test-delayed-queue", codec);
        return redissonClient.getDelayedQueue(blockingQueue);
    }

    //RingBuffer
    @Bean("test-ring-buffer")
    public RRingBuffer<Integer> rRingBuffer(RedissonClient redissonClient) {
        return redissonClient.getRingBuffer("test-ring-buffer");
    }

    //TransferQueue
    @Bean("test-transfer-queue")
    public RTransferQueue<String> rTransferQueue(RedissonClient redissonClient) {
        return redissonClient.getTransferQueue("test-transfer-queue");
    }

}
